﻿using BitButterCORE.Extension;

namespace BitButterCORE.Object
{
	public abstract class BasePersistentHeader
	{
		public BasePersistentHeader(string filepath)
			: this(filepath, 0, null)
		{
		}

		public BasePersistentHeader(string filepath, uint headerLength, byte[] headerPayload)
		{
			FilePath = filepath;
			Length = headerLength;
			Payload = headerPayload;
		}

		public string FilePath { get; }

		public uint Length { get; private set; }

		public byte[] Payload { get; private set; }

		public byte[] Data
		{
			get
			{
				if (data == null && Length > 0)
				{
					data = Payload.PrependLength();
				}
				return data;
			}
		}
		byte[] data;

		public void SetPersistentData(byte[] headerData)
		{
			Length = (uint?)headerData?.Length ?? 0;
			Payload = headerData;
			data = null;
		}
	}
}
